package com.fub.guidance;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;

import com.fub.update.BuildingVersionMap;
import com.fub.util.database.databaseHelper.BuildingVersionsDatabaseManager;

public class BuildingVersionDAO {
	private SQLiteOpenHelper buildingVersionDBManager;

	/**
	 * Constructs a new data access object for version data.
	 * 
	 * @param context
	 *            the application context
	 */
	public BuildingVersionDAO(Context context) {
		this.buildingVersionDBManager = new BuildingVersionsDatabaseManager(
				context);
	}

	/**
	 * Returns the id of all buildings in database.
	 * 
	 * @return the id of all buildings in database
	 */
	public String[] getAllLocalBuildingIds() {
		BuildingVersionMap buildingMap = getAllBuildingVersionsMap();
		String[] result = new String[buildingMap.size()];
		int i = 0;
		for (String buildingId : buildingMap.getBuildingsIds()) {
			result[i++] = buildingId;
		}
		return result;
	}

	/**
	 * Returns the id of all buildings in database.
	 * 
	 * @return the id of all buildings in database
	 */
	public BuildingVersionMap getAllBuildingVersionsMap() {
		BuildingVersionMap buildingVersionMap = new BuildingVersionMap();
		String query = String.format("SELECT * FROM %s",
				BuildingVersionsDatabaseManager.TABLE_BUILDING_VERSIONS);
		Cursor cursor = this.buildingVersionDBManager.getReadableDatabase()
				.rawQuery(query, null);
		while (cursor.moveToNext()) {
			String buildingId = cursor.getString(0);
			int version = cursor.getInt(1);
			float latitude = cursor.getFloat(2);
			float longitude = cursor.getFloat(3);
			buildingVersionMap.addBuilding(buildingId, version, latitude,
					longitude);
		}
		cursor.close();
		return buildingVersionMap;
	}

	/**
	 * Returns true, if the building is under version control.
	 * 
	 * @param buildingId
	 *            true, if the building is under version control
	 * @return
	 */
	public boolean isBuildingInVersionControll(String buildingId) {
		String[] allBuildingIds = getAllLocalBuildingIds();
		for (int i = 0; i < allBuildingIds.length; i++) {
			if (allBuildingIds[i].equals(buildingId)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Inserts a building into version control.
	 * 
	 * @param buildingID
	 *            the id of the building
	 */
	public void insertBuildingToVersionTable(String buildingID, float latitude,
			float longitude) {
		String query = String.format(
				"INSERT INTO %s (%s,%s,%s,%s) VALUES ('%s','%s','%s','%s')",
				BuildingVersionsDatabaseManager.TABLE_BUILDING_VERSIONS,
				BuildingVersionsDatabaseManager.COL_BUILDING_ID,
				BuildingVersionsDatabaseManager.COL_VERSION,
				BuildingVersionsDatabaseManager.COL_LATITUDE,
				BuildingVersionsDatabaseManager.COL_LONGITUDE, buildingID,
				BuildingVersionsDatabaseManager.DEFAULT_BUILDING_VERSION,
				latitude, longitude);
		this.buildingVersionDBManager.getWritableDatabase().execSQL(query);
	}

	/**
	 * Sets the version number of a building on a default value.
	 * 
	 * @param buildingID
	 *            the id of the building
	 */
	public void resetBuildingInVersionControll(String buildingID) {
		String query = String.format("UPDATE %s SET %s  ='%s' WHERE %s = '%s'",
				BuildingVersionsDatabaseManager.TABLE_BUILDING_VERSIONS,
				BuildingVersionsDatabaseManager.COL_VERSION,
				BuildingVersionsDatabaseManager.DEFAULT_BUILDING_VERSION,
				BuildingVersionsDatabaseManager.COL_BUILDING_ID, buildingID);
		this.buildingVersionDBManager.getWritableDatabase().execSQL(query);
	}

	/**
	 * Updates the building version.
	 * 
	 * @param buildingId
	 *            the id of the building
	 * @param newVersion
	 *            the new version number
	 */
	public void updateBuildingVersion(String buildingId, int newVersion) {
		String query = String.format("UPDATE %s SET %s='%s' WHERE %s = '%s' ",
				BuildingVersionsDatabaseManager.TABLE_BUILDING_VERSIONS,
				BuildingVersionsDatabaseManager.COL_VERSION, newVersion,
				BuildingVersionsDatabaseManager.COL_BUILDING_ID, buildingId);
		this.buildingVersionDBManager.getWritableDatabase().execSQL(query);
	}

	/**
	 * Deletes all buildings from version control.
	 */
	public void deleteAllVersions() {
		String query = String.format("DELETE FROM %s ",
				BuildingVersionsDatabaseManager.TABLE_BUILDING_VERSIONS);
		this.buildingVersionDBManager.getWritableDatabase().execSQL(query);
	}
}